﻿Imports Entidad
Imports Utilitario
Imports System.Data.SqlClient
Public Class ConteoDatos
    Inherits ConexionSQL

    Public Function ConsultarConteo(ByVal cia As String, ByVal CONT_Codigo As String, ByVal ART_Codigo As String, ByVal ART_Ubicacion As String) As Conteo
        Try
            conectarSQL()
            Dim da As New SqlDataAdapter("", cnn)
            Dim ds As New DataSet
            da.SelectCommand.CommandText = "SELECT CIA, CONT_Codigo, ART_Codigo, ART_Ubicacion, CONT_Grupo, CONT_Cantidad FROM Conteo_Articulos " & _
            " WHERE CIA = '" & cia & "' and CONT_Codigo = '" & CONT_Codigo & "' and ART_Codigo = '" & ART_Codigo & "' and ART_Ubicacion ='" & ART_Ubicacion & "'"

            da.Fill(ds, "Conteo")
            Dim conteo As Conteo
            conteo = Nothing
            For Each dr As DataRow In ds.Tables(0).Rows
                conteo = New Conteo
                With conteo
                    .gCIA = If(dr("CIA") Is DBNull.Value, "", dr("CIA"))
                    .gCONT_Codigo = If(dr("CONT_Codigo") Is DBNull.Value, "", dr("CONT_Codigo"))
                    .gART_Codigo = If(dr("ART_Codigo") Is DBNull.Value, "", dr("ART_Codigo"))
                    .gART_Ubicacion = If(dr("ART_Ubicacion") Is DBNull.Value, "", dr("ART_Ubicacion"))
                    .gCONT_Grupo = If(dr("CONT_Grupo") Is DBNull.Value, "", dr("CONT_Grupo"))
                    .gCONT_Cantidad = If(dr("CONT_Cantidad") Is DBNull.Value, "", dr("CONT_Cantidad"))
                End With
            Next

            Return conteo
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            desconectarSQL()
        End Try
    End Function

    Public Function registrarConteo(ByVal cia As String, ByVal CONT_Codigo As String, ByVal ART_Codigo As String, ByVal ART_Ubicacion As String, ByVal CONT_Grupo As String,
                               ByVal CONT_Cantidad As Double) As Boolean
        Try
            conectarSQL()
            Dim comando As New SqlCommand("INSERT INTO Conteo_Articulos(CIA, CONT_Codigo, ART_Codigo, ART_Ubicacion, CONT_Grupo, CONT_Cantidad) " & _
                                     "VALUES('" & cia & "', '" & CONT_Codigo & "', '" & ART_Codigo & "', '" & ART_Ubicacion & "', '" & CONT_Grupo & "'," & _
                                     CONT_Cantidad & ")", cnn)
            comando.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectarSQL()
        End Try
        Return True
    End Function

    Public Function ActualizarConteo(ByVal cia As String, ByVal CONT_Codigo As String, ByVal ART_Codigo As String, ByVal ART_Ubicacion As String, ByVal CONT_Grupo As String,
                               ByVal CONT_Cantidad As Double) As Boolean
        Try
            conectarSQL()
            Dim comando As New SqlCommand("UPDATE Conteo_Articulos SET  CONT_Grupo = '" & CONT_Grupo & "', CONT_Cantidad = " & CONT_Cantidad & _
                                          "WHERE CIA = '" & cia & "' AND CONT_Codigo = '" & CONT_Codigo & "' AND ART_Codigo = '" & ART_Codigo & "' AND " &
                                          "ART_Ubicacion = '" & ART_Ubicacion & "'", cnn)
            comando.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        Finally
            desconectarSQL()
        End Try
        Return True
    End Function
End Class
